/*
*
* METROVIEW
*
*/

// IMPORT LIBRARIES

import processing.pdf.*;
import controlP5.*;
import org.gicentre.utils.move.*;
import org.gicentre.utils.colour.*;
import org.gicentre.utils.spatial.*;

// DECLARE GLOBAL VARIABLES

ZoomPan zoomer;
ControlP5 controlP5;
PDFUI pdfui;
MMUI mmui;
StaticUI sui;
ScaleUI scal;
ModeSelectorUI msui;
NHoodInfoUI nhui;
PDFMaker pdfm;
Poll[] polls;
float[][] maxMins;
int dMode;            
color dFil;    
color noDataFil;    
color strok; 
DModeManager dmm;
Ellipsoid wgs84;
UTM utmConverter;
PVector localOrigin;
float localWidth, localHeight;
float sw;
PFont pdfExportFontSmall, pdfExportFontLarge;
int singleWidth, singleHeight;

// -----------------------------------------------
// SETUP
// -----------------------------------------------

void setup() {
  
  // CREATE WINDOW
  
  //size(600, 700);
  size(11400, 4200); // 11400 = 600 * 19; 4200 = 700 * 6
  singleWidth = 600;
  singleHeight = 700;
  background(255);
   
  // READ DATA
  
  // load font
  pdfExportFontSmall = createFont("LetterGothicStd-Bold", 9, true);
  pdfExportFontLarge = createFont("LetterGothicStd-Bold", 15, true);
  //println(PGraphicsPDF.listFonts());
  
  // read kml file
  KMLReader kmlr = new KMLReader(this);
  
  // read neighbourhood kml and csv files
  String[][] kmlData = kmlr.readKML();
  String[][] electionData = kmlr.readElectionInfo();
  
  // ASSIGN GLOBAL VARIABLES
  
  // create UTM converter to convert lat/long coordinates to UTM, and use it create local origin
  wgs84 = new Ellipsoid(Ellipsoid.WGS_84);  // WGS-84 is apparently the most commonly used ellipsoid for projections of this type
  utmConverter = new UTM(wgs84, 12, 'U');   // Edmonton's UTM zone is 12U
  PVector originll = new PVector(-113.72, 53.37); // this controls the starting position
  localOrigin = utmConverter.latLongToUTM(originll);
  
  // set local width and height
  localWidth = 35000; // units are in meters as per UTM specifications; this controls starting "zoom"
  localHeight = localWidth * singleHeight / singleWidth;
  
  // create neighbourhood objects and calculate metrics
  PollMaker pm = new PollMaker(kmlData, electionData);
  polls = pm.makePolls();     
  
  // create drawing mode manager and set startup drawing mode
  dmm = new DModeManager(polls);
  dmm.setDMode(0);
  
  // create various UI components
  pdfm = new PDFMaker();                                                      // controls PDF creation in sketch
  controlP5 = new ControlP5(this);                                            // "parent" UI object
  sui = new StaticUI(controlP5, this);                                        // object encapsulating static UI components
  scal = new ScaleUI(controlP5, this, 10, 44, 20, 60);                        // controls falsecolour scale bar
  //msui = new ModeSelectorUI(controlP5, this, dmm, 6, 187, 225, 300);          // controls drawing mode selector
  msui = new ModeSelectorUI(controlP5, this, dmm, 6, 171, 169, 15);
  nhui = new NHoodInfoUI(controlP5, 6, 124, 225, 43);                         // "neighbourhood info" box that displays neighbourhood info based on mouse position
  pdfui = new PDFUI(controlP5, this, 79, 78, 80, 15);                         // create pdf button
  mmui = new MMUI(controlP5, this, 79, 98, 80, 15);                         // create max/min mode toggle button
  zoomer = new ZoomPan(this);                                                 // object for zooming and panning 
  zoomer.setZoomMouseButton(PConstants.RIGHT);                                // set mouse button for zooming/panning
  
  // SET GRAPHIC PARAMETERS
  
  smooth();
  sw = 0.2;
  strokeWeight(sw);
  dFil = color(200, 200, 200);     // "default" fill
  noDataFil = color(241, 241, 241);    // "no data" fill -- used to fill neighbourhoods when the neighbourhood has no data
  strok = color(180, 180, 180, 100);     // "default" stroke
  
  // DRAW PDFS
  
  // declare placeholder for pdf export paths
  //String path;
  String path = "/Users/macuser/Desktop/out.pdf";
  
  // declare translation variables
  int tx, ty;
  
  // begin pdf recording
  beginRecord(PDF, path);
  
  // draw background
  background(255); 
  
  // iterate through drawing modes and draw each to pdf
  for(int i = 1; i < dmm.dModes.size(); i++) {
    
    println("Printing: " + i);
     
    // SETUP
    
    // set DMode
    dmm.setDMode(i);
  
    // DRAW MAP
    
    // store current position
    pushMatrix();
    
    // calculate translation and apply
    // (move left to right, top to bottom)
    tx = 600 * ( (i - 1) % 19 );
    ty = 700 * floor( (i - 1) / 19);
    translate(tx, ty);
    
    // store translated position
    pushMatrix();
    
    // rotate neighbourhoods to align lat/long w/ horizontal/vertical
    float rot = -PI/80;
    rotate(rot);
    
    // draw neighbourhoods
    for(int j = 0; j < polls.length; j++) {
      polls[j].draw(dmm);
    }

    // undo UTM correctional rotation before drawing scale and title
    popMatrix();     
    
    // draw static UI componenets, checking if pdf writing is on.
    // If so, write UI components for pdf export, and turn off pdf recording
    sui.draw(true);
    scal.draw(dmm, true);
    
    // restore initial position
    popMatrix();
    
  } // for
  
  // stop pdf recording
  endRecord();
  
  // exit program
  exit();
  
} // setup


